import random

from bald_spider.exceptions import IgnoreRequest
from bald_spider.middleware import BaseMiddleware


class BaiduSpiderMiddleware(BaseMiddleware):

    async def process_request(self,request,spider):
        # 预处理请求前
        # print("process_request",request,spider)
        if random.randint(1,5) ==1:
            raise IgnoreRequest("url不规则")

    def process_response(self, request, response, spider):
        # 预处理响应后
        # print("process_response",request,response, spider)
        if random.randint(1,5) ==1:
            raise IgnoreRequest("脏数据")
        return response

    def process_exception(self, request, exception, spider):
        # 处理异常
        print("process_exception",request,exception,spider)
        # if isinstance(exception,IgnoreRequest):
        #     return True